package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import DAO.LogDAO;

import com.et.mvc.JsonView;

/**
 * Servlet implementation class selectLogServlet
 */
public class selectLogServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public selectLogServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		int start = Integer.parseInt(request.getParameter("start"));
        int limit = Integer.parseInt(request.getParameter("limit"));
		String assetName = request.getParameter("assetName");
		
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/plain");
		PrintWriter out = response.getWriter();	
		
		LogDAO getLogDAO = new LogDAO();
		@SuppressWarnings("unchecked")
		List getLogSelectList = null;
        Map<String,Object> map = new HashMap<String,Object>();
        
        if(assetName.equals(""))
        {
        	getLogSelectList = getLogDAO.getAllLog(start, limit);       	         	 
        }
        else
        {
        	getLogSelectList = getLogDAO.getLogByQuery(start, limit, assetName);      	
        }
        map.put("total",getLogDAO.count);
        map.put("result",getLogSelectList);  
        
        out.print(new JsonView(map));		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request,response);
	}

}
